﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MonitorPlan.Admin
{
    public partial class House : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                if(Session["userid"]==null)
                {
                    Response.Redirect("/Admin");
                }
                if (Session["new_house"] != null)
                {
                    this.mv.ActiveViewIndex = 1;
                    Session.Remove("new_house");
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.mv.ActiveViewIndex = 1;
            this.txtHouseName.Text = "";
            this.txtHouseDescription.Text = "";
            this.actionSms.InnerHtml = "";
            this.txtHouseName.Focus();
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            this.rp1.DataBind();
            this.mv.ActiveViewIndex = 0;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.txtHouseName.Text = "";
            this.txtHouseDescription.Text = "";
            this.txtHouseName.Focus();
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            this.rp1.DataBind();
            this.mv.ActiveViewIndex = 0;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            String sms;
            Model.House h = new Model.House();
            h.Name = this.txtHouseName.Text;
            h.Description = this.txtHouseDescription.Text;
            h.OwnerId = Convert.ToInt32(this.txtHouseOwner.SelectedValue);
            h.LocationId = Convert.ToInt32(this.txtHouseLocation.SelectedValue);
            if(this.txtHouseName.Text !="")
            {
                if(h.Add())
                {
                    sms = "<div class='alert alert-success'>";
                    sms += "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                    sms += "Data has been save successfully!";
                    sms += "</div>";
                    this.txtHouseName.Text = "";
                    this.txtHouseDescription.Text = "";
                    this.txtHouseName.Focus();
                    this.actionSms.InnerHtml = sms;
                }
                else
                {
                    sms = "<div class='alert alert-error'>";
                    sms += "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                    sms += "Data has not been saved successfully. Try again!";
                    sms += "</div>";
                    this.actionSms.InnerHtml = sms;
                }
            }
            else
            {
                this.actionSms.InnerHtml = "<span style='color:red'>Please input house name!</span>";
            }
        }

        protected void btnAddLocation_Click(object sender, EventArgs e)
        {
            Session["new_location"] = "new";
            Response.Redirect("/Admin/Location.aspx");
        }

        protected void rp1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "detail")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                Model.House h = new Model.House().getHouse(id);
                this.lbHouseId.Text = h.Id.ToString();
                this.lbHouseName.Text = h.Name;
                this.lbHouseDescription.Text = h.Description;
                this.lbOwnerName.Text = h.OwnerName;
                this.lbLocationName.Text = h.LocationName;
                this.lbIsDeleted.Text = h.IsDeleted.ToString();
                this.mv.ActiveViewIndex = 2;

            }
            if(e.CommandName=="delete")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                Model.House h = new Model.House();
                if(h.Remove(id))
                {
                    this.rp1.DataBind();
                    this.mv.ActiveViewIndex = 0;
                }
            }
            if(e.CommandName=="edit")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                this.mv.ActiveViewIndex = 3;
                Model.House h = new Model.House().getHouse(id);
                this.txtHname.Text = h.Name;
                this.txtOwnerName.SelectedIndex = this.txtOwnerName.Items.IndexOf(this.txtOwnerName.Items.FindByText(h.OwnerName));
                this.txtLocation.SelectedIndex = this.txtLocation.Items.IndexOf(this.txtLocation.Items.FindByText(h.LocationName));
                this.txtHDescription.Text = h.Description;
                this.txtid.Value = id.ToString();
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            this.mv.ActiveViewIndex = 0;
        }

        protected void btnCancelUpdate_Click(object sender, EventArgs e)
        {
            this.mv.ActiveViewIndex = 0;
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if(this.txtHname.Text!="")
            {
                Model.House h = new Model.House();
                h.Name = this.txtHname.Text;
                h.OwnerId = Convert.ToInt32(this.txtOwnerName.SelectedValue);
                h.LocationId = Convert.ToInt32(this.txtLocation.SelectedValue);
                h.Description = this.txtHDescription.Text;
                h.Id = Convert.ToInt32(this.txtid.Value);
                int id = Convert.ToInt32(this.txtid.Value);
                if(h.Update(id))
                {
                    this.rp1.DataBind();
                    this.mv.ActiveViewIndex = 0;
                }
                else
                {
                    this.updateSms.InnerHtml = "Cannot update data. Try again!";
                }

            }
        }

        protected void btnNewLocation_Click(object sender, EventArgs e)
        {
            Session["new_location"] = "new";
            Response.Redirect("/Admin/Location.aspx");
        }
    }
}